package com.zkthink.ceres.common.utils;

import net.sf.json.JSONObject;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HttpUtils
 * @Description: HttpUtils
 * @Author ck
 * @Date 2021/9/8
 * @Version V1.0
 **/
public class HttpUtils {
    /**
     * http post请求, 带参数
     *
     * @param requestURL
     * @param params
     * @return
     */
    public static String doPost(String requestURL, String host, String appKey, String nonce, String timeStamp, String signature, String params, String proxyHost, Integer proxyPort) {
        // 记录信息
        StringBuffer buffer = new StringBuffer();

        HttpURLConnection conn = null;
        try {
            URL url = new URL(requestURL);
            // 判断是否需要代理模式请求http
            if (proxyHost != null && proxyPort != null) {
                // 如果是本机自己测试, 不需要代理请求,但发到服务器上的时候需要代理请求
                // 对http开启全局代理
                //System.setProperty("http.proxyHost", proxyHost);
                //System.setProperty("http.proxyPort", proxyPort);
                // 对https开启全局代理
                //System.setProperty("https.proxyHost", proxyHost);
                //System.setProperty("https.proxyPort", proxyPort);

                // 代理访问http请求
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                conn = (HttpURLConnection) url.openConnection(proxy);
            } else {
                // 原生访问http请求，未代理请求
                conn = (HttpURLConnection) url.openConnection();
            }

            // 设置请求的属性
            conn.setDoOutput(true); // 是否可以输出
            conn.setRequestMethod("POST"); // 请求方式, 只包含"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE"六种
            conn.setConnectTimeout(60000); // 最高超时时间
            conn.setReadTimeout(60000); // 最高读取时间
            conn.setConnectTimeout(60000); // 最高连接时间
            conn.setRequestProperty("Host", host);
            conn.setRequestProperty("RC-App-Key", appKey);
            conn.setRequestProperty("RC-Nonce", nonce);
            conn.setRequestProperty("RC-Timestamp", timeStamp);
            conn.setRequestProperty("RC-Signature", signature);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", "78");
            conn.setDoInput(true); // 是否可以输入
            if (params != null) {
                // 设置参数为json格式
                conn.setRequestProperty("Content-type", "application/json");
                // 写入参数信息
                OutputStream os = conn.getOutputStream();
                try {
                    os.write(params.getBytes("UTF-8"));
                } catch (Exception e) {
                    System.out.println("HttpPostUtils doPost error: " + e);
                } finally {
                    try {
                        if (os != null) {
                            os.close();
                        }
                    } catch (IOException e) {
                        System.out.println("HttpPostUtils doPost error: " + e);
                    }
                }
            }

            URL url1 = conn.getURL();
            System.out.println(url1);
            System.out.println(params);
            // 读取数据
            InputStream is = null;
            InputStreamReader inputReader = null;
            BufferedReader reader = null;
            try {
                is = conn.getInputStream();
                inputReader = new InputStreamReader(is, "UTF-8");
                reader = new BufferedReader(inputReader);

                String temp;
                while ((temp = reader.readLine()) != null) {
                    buffer.append(temp);
                }
            } catch (Exception e) {
                System.out.println("HttpPostUtils doPost error: " + e);
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                    if (inputReader != null) {
                        inputReader.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    System.out.println("HttpPostUtils doPost error: " + e);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 当http连接空闲时, 释放资源
            if (conn != null) {
                conn.disconnect();
            }
        }
        // 返回信息
        return buffer.length() == 0 ? "" : buffer.toString();
    }


    public static void main(String[] args) {
        String params = null;
        Map<String, Object> map = new HashMap();
//        map.put("access_token","24.ad78836b0476d2f4fe5b05df3a365d3d.2592000.1633666314.282335-24761352");
        List list = new ArrayList();
        list.add("6138574bcc3deca2ffc3288b");
        map.put("task_ids", list);
        System.out.println(JSONObject.fromObject(map).toString());
//        String s = doPost("https://aip.baidubce.com/rpc/2.0/aasr/v1/query?access_token=24.ad78836b0476d2f4fe5b05df3a365d3d.2592000.1633666314.282335-24761352", JSONObject.fromObject(map).toString(),null,null);
//        System.out.println(s);
    }
}
